/*
	This file is part of "Ants on the web".

    "Ants on the web" is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    "Ants on the web" is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with "Ants on the web".  If not, see <http://www.gnu.org/licenses/>.
*/

package org.utc.ia04.antsontheweb.main;

import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.wrapper.AgentController;
import jade.wrapper.ContainerController;
import java.util.Scanner;


public class DefaultClient
{
	//Dossier courant
	public static String LOCAL_FOLDER = "";
	
	//Option de choix
	private static final String NEW_MAIN_CONTAINER = "NEW";
	
	//Main Method
	public static void main(String[] args)
	{
		//A) CONFIGURATION DU SYSTEM
		LOCAL_FOLDER = System.getProperty("user.dir") + "/externalData/";
		
		//B) PRESENTATION DU CLIENT
		System.out.print("Bienvenue sur client de Ants On The Web...\n\n");
			
		//C) CHOIX DES OPTIONS
		boolean connectionEstablished = false;
		ContainerController cc = null;
		
		while(!connectionEstablished)
		{
			try
			{
				Scanner scanner = new Scanner(System.in);
				System.out.println("Veuillez saisir l'adresse du client ou saisir " + DefaultClient.NEW_MAIN_CONTAINER +  " pour créer un conteneur principal :");
				String adresse = scanner.nextLine();
				boolean isMainController = adresse.toUpperCase().equals(DefaultClient.NEW_MAIN_CONTAINER);
				
				cc = isMainController
						? createMainContainer()
						: connectToContainer(adresse);
				
				//D) CREATION DE L'AGENT
				int index = 1;
				boolean started = false;
						
				while(index < 100 && !started)
				{
					try
					{
						Object[] argInterface = new Object[1];
						argInterface[0] = Boolean.valueOf(isMainController);
						
						AgentController ac = cc.createNewAgent("Interface_Principale_" + String.valueOf(index), "org.utc.ia04.antsontheweb.gui.request.AgentInterfaceRequest", argInterface);
						ac.start();
						started = true;
					}
					catch(Exception e2) { ++index; }
				}
						
				//D) NEED TO CONTINUE ?
				if(index != 100) 	connectionEstablished = true;
				else 				System.out.print("Connection failed... \n");
			}
			catch(Exception e) { }
		}
		
		//END OF DEFAULT CLIENT MAIN FUNCTION
	}
	
	private static ContainerController createMainContainer() throws Exception
	{
		jade.core.Runtime rt = jade.core.Runtime.instance();
		Profile p = new ProfileImpl(LOCAL_FOLDER + "shortmain.properties");
		
		/*----FIX LINUX
		p.setParameter(ProfileImpl.LOCAL_HOST, "127.0.0.1");
		//------------*/
		
		return rt.createMainContainer(p);	
	}
	
	private static ContainerController connectToContainer(String adresse) throws Exception
	{
		Runtime rt = Runtime.instance();
		Profile p = new ProfileImpl(adresse,-1,null,false);
				
		/*----FIX LINUX
		p.setParameter(ProfileImpl.LOCAL_HOST, "127.0.0.1");
		//------------*/

		return rt.createAgentContainer(p);
	}
}
